"""
file: ShoppingCartPage 购物车页面
author: 方晓燚
date: 2022/1/6
"""

# 引入包和模块
from Common.Base import Base, open_browser
import unittest
from Page.LoginPage import LoginPage

# 网址
shopping_url = 'http://localhost:8080/ecshop/user.php'


class ShoppingCart(Base):
    """封装表现层,制作定位器"""
    shoopingcart_loc = ('class name', 'cart')  # 定位购物车按钮
    clearcart_loc = (
        'css selector',
        '#formCart > table:nth-child(2) > tbody > tr > td:nth-child(2) > input:nth-child(1)')  # 定位清空购物车按钮
    updatecart_loc = ('name', 'submit')  # 定位更新购物车按钮
    goshooping_loc = ('xpath', '//img[@alt="continue"]')  # 定位继续购物按钮
    input_loc = ('name', 'keywords')  # 定位搜索框按钮
    delete_loc = ('xpath', '//*[@id="formCart"]/table[1]/tbody/tr[2]/td[7]/a')  # 定位删除按钮
    search_loc = ('name', 'imageField')  # 定位搜索按钮
    goods_loc = ('css selector', '.goodsItem>a')  # 定位商品详情元素
    buy_loc = ('css selector', '.td1>a')  # 定位立即购买按钮
    collect_loc = ('xpath', '//*[@id="formCart"]/table[1]/tbody/tr[2]/td[7]/a[2]')  # 定位放入收藏夹
    close_money_loc = ('xpath', '/html/body/div[6]/div[1]/table/tbody/tr/td[2]/a')  # 定位结算按钮
    input_number_loc = ('css selector', 'input[onkeydown="showdiv(this)"]')  # 定位购买数量输入框

    # 用于断言1
    goods_01_loc = ('css selector', 'div[class="goods_style_name"]')   # 定位商品页面商品名称文本值
    result_loc = ('class name', 'f6')  # 定位加入购物车后的商品名称文本值

    # 用于断言2
    take_delivery_loc = ('xpath', '//*[@id="theForm"]/div/h6/span')   # 定位收货人信息

    # 用于断言3
    home_page_loc = ('xpath', '//*[@id="mainNav"]/div/ul/li[1]/a')   # 定位首页

    # 用于断言7
    goods_picture_loc = ('css selector', 'a[target="_blank"]>img')  # 定位商品图片


    def input_goods(self, text):
        """点击搜索框输入商品名称"""
        self.clear(self.input_loc)
        self.send_keys(self.input_loc, text)

    def click_search(self):
        """点击搜索按钮"""
        self.click(self.search_loc)

    def click_goos(self):
        """点击商品详情"""
        self.click(self.goods_loc)

    def click_buy(self):
        """点击立即购买"""
        return self.click(self.buy_loc)

    def click_cart(self):
        """点击购物车查看"""
        self.click(self.shoopingcart_loc)

    def click_clear_cart(self):
        """点击清空购物车"""
        self.click(self.clearcart_loc)

    def click_update_cart(self):
        """点击更新购物车"""
        self.click(self.updatecart_loc)

    def click_go_shooping(self):
        """点击继续购物"""
        return self.click(self.goshooping_loc)

    def click_del_goods(self):
        """点击删除商品"""
        self.click(self.delete_loc)

    def alert_accept(self):
        """点击弹窗中的确定按钮"""
        self.driver.switch_to.alert.accept()

    def alert_dismiss(self):
        """点击弹窗中的取消按钮"""
        self.driver.switch_to.alert.dismiss()

    def click_coollect(self):
        """点击放入收藏夹"""
        self.click(self.collect_loc)

    def click_close_money(self):
        """点击结算"""
        self.click(self.close_money_loc)

    def click_input_number(self, number):
        """点击购买数量输入框"""
        self.clear(self.input_number_loc)
        self.send_keys(self.input_number_loc, number)

# 用于断言
    def get_buygoods_text(self):
        """获取商品页面商品文本值"""
        return self.get_element_text(self.goods_01_loc)

    def result_text(self):
        """获取加入购物车后的商品文本值"""
        return self.get_element_text(self.result_loc)

    def take_delivery(self):
        """获取收获人信息文本值"""
        return self.get_element_text(self.take_delivery_loc)

    def home_page(self):
        """获取首页文本值"""
        return self.get_element_text(self.home_page_loc)

    def input_number(self):
        """获取输入框文本值"""
        return self.get_element_text(self.input_number_loc)


    def cart_text(self):
        """获取购物车文本值"""
        return self.get_element_text(self.shoopingcart_loc)

    def goods_picture(self):
        """点击商品图片"""
        self.click(self.goods_picture_loc)


if __name__ == '__main__':
    driver = open_browser()
    login = LoginPage(driver)
    login.open_url(shopping_url)
    login.input_username('fxy')
    login.input_password(123456)
    login.click_login()

    shooping = ShoppingCart(driver)
    shooping.input_goods('手机')
    shooping.click_search()
    shooping.click_goos()
    # shooping.get_buygoods_text()
    shooping.click_buy()

    # shooping.input_number()
    # shooping.click_cart()
    # shooping.click_coollect()
    # shooping.result_text()


    # shooping.click_input_number(2)
    # shooping.click_close_money()

    # shooping.alert_accept()
    # shooping.click_input_number(2)

    # shooping.click_go_shooping()
    # shooping.click_del_goods()
    # shooping.alert_accept()
    # shooping.alert_dismiss()
    # shooping.click_update_cart()
    # shooping.click_clear_cart()
